/**
 * 
 */
package com.nari.baseinfo.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.nari.baseinfo.dao.ICityDao;
import com.nari.baseinfo.dao.IProjectDao;
import com.nari.baseinfo.model.IProject;
import com.nari.basic.dao.IRoleDao;
import com.nari.basic.dao.IUserDao;
import com.nari.basic.dao.IUserRoleDao;
import com.nari.basic.model.IRole;
import com.nari.basic.model.IUser;
import com.nari.coreframe.service.GenericServiceImpl;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.resconfig.dao.IProjectResourceConfigDao;
import com.nari.resconfig.model.IProjectResourceConfig;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class ProjectServiceImpl extends GenericServiceImpl<IProject, String>
		implements IProjectService {
	private ICityDao iCityDao;
	private IUserDao iUserDao;
	private IRoleDao iRoleDao;
	private IUserRoleDao iUserRoleDao;
	private String projManagerRole;
	private IProjectResourceConfigDao iProjectResourceConfigDao;

	/**
	 * 分页查询项目列表
	 * @param projectID 项目编号(可选)
	 * @param projectName 项目名称(可选)
	 * @param capitalCityID 所属市级机构ID(可选)
	 * @param provinceCityID 所属省级机构ID(可选)
	 * @param projDate 施工日期(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-5
	 */
	public ResultWithPageCond<IProject> queryProjectWithPageCond(String projectID, 
			String projectName, String capitalCityID, String provinceCityID, Date projDate, 
			PageCond pageCond) throws Exception {
		IProjectDao iProjectDao = (IProjectDao) this.getiGenericDao();
		return iProjectDao.queryProjectWithPageCond(projectID, projectName, capitalCityID, 
				provinceCityID, projDate, pageCond);
	}
	
	/**
	 * 分页查询项目列表
	 * @param projectID 项目编号(可选)
	 * @param projectName 项目名称(可选)
	 * @param capitalCityUID 所属市级机构UID(可选)
	 * @param provinceCityUID 所属省级机构UID(可选)
	 * @param projDate 施工日期(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-14
	 */
	public ResultWithPageCond<IProject> queryAuthorityProjectWithPageCond(String projectID, 
			String projectName, String capitalCityUID, String provinceCityUID, Date projDate, 
			PageCond pageCond) throws Exception {
		IProjectDao iProjectDao = (IProjectDao) this.getiGenericDao();
		return iProjectDao.queryAuthorityProjectWithPageCond(projectID, projectName, 
				capitalCityUID, provinceCityUID, projDate, pageCond);
	}

	/**
	 * 通过项目列表查询与之关联的行政机构ID-机构名称映射表
	 * 
	 * @param projectList
	 *            项目列表，若列表为空，则返回空的映射表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-6
	 */
	public Map<String, String> queryReferCitysByProjList(
			List<IProject> projectList) throws Exception {
		List<String> cityIDList = null;
		if (projectList != null && projectList.size() > 0) {
			cityIDList = new ArrayList<String>();
			for (IProject project : projectList) {
				cityIDList.add(project.getCapitalCityID());
				cityIDList.add(project.getProvinceCityID());
			}
		}
		return iCityDao.queryIdNameMapByIDList(cityIDList);
	}

	/**
	 * 查询所有项目信息，并对结果集排序
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-8
	 */
	public List<IProject> queryAllProjectInOrder() throws Exception {
		IProjectDao iProjectDao = (IProjectDao) this.getiGenericDao();
		return iProjectDao.queryAllProjectInOrder();
	}

	/**
	 * 通过所属城市查询项目列表
	 * 
	 * @param provinceCityID
	 *            所属省级机构
	 * @param capitalCityID
	 *            所属市级机构
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-25
	 */
	public List<IProject> queryProjectsByBelongCity(String provinceCityID,
			String capitalCityID) throws Exception {
		IProjectDao iProjectDao = (IProjectDao) this.getiGenericDao();
		return iProjectDao.queryProjectsByBelongCity(provinceCityID,
				capitalCityID);
	}

	/**
	 * 通过唯一标识列表查询项目信息
	 * @param idList 唯一标识列表，若为空列表，则结果集返回空
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-9
	 */
	public List<IProject> queryProjectsByIDList(List<String> idList)
			throws Exception {
		IProjectDao iProjectDao = (IProjectDao) this.getiGenericDao();
		return iProjectDao.queryProjectsByIDList(idList);
	}

	/**
	 * 根据项目ID级联删除关联信息
	 * 包括：项目、小区、楼栋、单元、热用户、系统用户及角色、集中器、热量表、分配表、散热器、
	 * 分摊域、分摊域分摊账单、用户分摊账单
	 * 注意：热量表数据、分配表数据等表计数据具有不可恢复性，因此不能作为级联删除的对象
	 * @param projectUID
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-1-13
	 */
	public void deleteCascadeByProjectUID(String projectUID) throws Exception {
		IProjectDao iProjectDao = (IProjectDao) this.getiGenericDao();
		iProjectDao.deleteCascadeByProjectUID(projectUID);
		//删除与项目有关的资源权限设置
		iProjectResourceConfigDao.deleteProjResAuthorityByProjInfo(projectUID);
	}
	
	/**
	 * 统计项目编号重复的个数
	 * @param projectID
	 * @param projectUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-11-24
	 */
	public int countRepeatProjectIDNum(String projectID, String projectUID) throws Exception {
		IProjectDao iProjectDao = (IProjectDao) this.getiGenericDao();
		return iProjectDao.countRepeatProjectIDNum(projectID, projectUID);
	}
	
	/**
	 * 查询系统中的项目管理员用户列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-1-23
	 */
	public List<IUser> queryProjManagerList() throws Exception {
		List<IUser> projManagerList = this.iUserDao.queryUserListByRoleID(projManagerRole);
		return projManagerList;
	}
	
	/**
	 * 新增或修改项目信息
	 * @param project
	 * @param projManagerHandler 人工录入的项目管理员
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-1-23
	 */
	public ResponseMsg saveOrUpdateProject(IProject project, String 
			projManagerHandler) throws Exception {
		//判断项目编号是否重复
		int repeatCounter = this.countRepeatProjectIDNum(project.getProjectID(), project.getId());
		if(repeatCounter > 0) {
			return ResponseMsg.getInstance(false, "重复的项目编号【"+project.getProjectID()+"】，项目信息保存失败");
		}
		//查询项目管理员角色
		IRole managerRole = iRoleDao.queryRoleInfoByRoleID(this.projManagerRole);
		if(managerRole == null) {
			return ResponseMsg.getInstance(false, "分摊管理员角色不存在，请联系系统管理员添加角色编号为【"+projManagerRole+"】的分摊管理员角色信息！");
		}
		//设置项目管理员
		if(projManagerHandler!=null && !"".equals(projManagerHandler.trim())) {
			IUser projManager = iUserDao.saveOrUpdateUser(projManagerHandler);
			project.setShareAreaManager(projManager.getId());
			//设置项目管理员角色
			this.iUserRoleDao.saveUserRoleAuthority(managerRole.getId(), projManager.getId());
		}
		//更新项目信息
		if (project.getId() != null && !"".equals(project.getId())) {
			this.getiGenericDao().updateEntity(project, project.getId());
		} else {
			this.getiGenericDao().saveEntity(project);
		}
		//设置管理员的项目资源权限
		List<IProjectResourceConfig> configList = iProjectResourceConfigDao.queryProjResByRoleInfo(project.getId(), managerRole.getId());
		if(configList!=null && configList.size()>0) {
			IProjectResourceConfig config = configList.get(0);
			config.setUserUID(project.getShareAreaManager());
			iProjectResourceConfigDao.updateEntity(config, config.getId());
		} else {
			iProjectResourceConfigDao.saveAuthorityUserBatch(project.getId(), managerRole.getId(), project.getShareAreaManager());
		}
		return ResponseMsg.getInstance(true, "");
	}

	/**
	 * 删除项目管理员
	 * @param projectUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	public ResponseMsg deleteProjectManager(String projectUID) throws Exception {
		IProjectDao iProjectDao = (IProjectDao) this.getiGenericDao();
		return iProjectDao.deleteProjectManager(projectUID);
	}
	
	/**
	 * 设置项目管理员
	 * @param projectUID
	 * @param userUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	public ResponseMsg saveProjectManager(String projectUID, String userUID) throws Exception {
		IProjectDao iProjectDao = (IProjectDao) this.getiGenericDao();
		return iProjectDao.saveProjectManager(projectUID, userUID);
	}
	
	/**
	 * @return the iCityDao
	 */
	public ICityDao getiCityDao() {
		return iCityDao;
	}

	/**
	 * @param iCityDao
	 *            the iCityDao to set
	 */
	public void setiCityDao(ICityDao iCityDao) {
		this.iCityDao = iCityDao;
	}

	public IUserDao getiUserDao() {
		return iUserDao;
	}

	public void setiUserDao(IUserDao iUserDao) {
		this.iUserDao = iUserDao;
	}

	/**
	 * @return the iRoleDao
	 */
	public IRoleDao getiRoleDao() {
		return iRoleDao;
	}

	/**
	 * @param iRoleDao the iRoleDao to set
	 */
	public void setiRoleDao(IRoleDao iRoleDao) {
		this.iRoleDao = iRoleDao;
	}

	public IUserRoleDao getiUserRoleDao() {
		return iUserRoleDao;
	}

	public void setiUserRoleDao(IUserRoleDao iUserRoleDao) {
		this.iUserRoleDao = iUserRoleDao;
	}

	/**
	 * @return the projManagerRole
	 */
	public String getProjManagerRole() {
		return projManagerRole;
	}

	/**
	 * @param projManagerRole the projManagerRole to set
	 */
	public void setProjManagerRole(String projManagerRole) {
		this.projManagerRole = projManagerRole;
	}

	/**
	 * @return the iProjectResourceConfigDao
	 */
	public IProjectResourceConfigDao getiProjectResourceConfigDao() {
		return iProjectResourceConfigDao;
	}

	/**
	 * @param iProjectResourceConfigDao the iProjectResourceConfigDao to set
	 */
	public void setiProjectResourceConfigDao(
			IProjectResourceConfigDao iProjectResourceConfigDao) {
		this.iProjectResourceConfigDao = iProjectResourceConfigDao;
	}

}
